#ifndef VECTOR_H
#define VECTOR_H

template <typename T>
class Vector
{
public:
    Vector();
    Vector(const Vector& other);
    ~Vector();

    void PushBack(const T& val);
    int Size() const;
    bool IsEmpty() const;

    T& operator[] (int index) const;

private:
    int len;
    int siz;
    int tail_pointer;
    T *array;
};

template <typename T>
Vector<T>::Vector()
{
    this->len = 0;
    this->siz = 1;
    this->tail_pointer = 0;
    this->array = new T[1];
}

template <typename T>
Vector<T>::Vector(const Vector& other) {
    this->len = other.len;
    this->siz = other.siz;
    this->tail_pointer = other.tail_pointer;
    this->array = new T[other.siz];
    for (int i = 0; i < other.len; i++) {
        this->array[i] = other.array[i];
    }
}

template <typename T>
Vector<T>::~Vector() {
    delete this->array;
}

template <typename T>
void Vector<T>::PushBack(const T& val) {
    // 内存不足,需要扩充内存
    if (this->len >= this->siz) {
        // 扩充内存
        this->siz <<= 1;
        T* tmp = new T[this->siz];
        // 复制内容
        for (int i = 0; i < this->len; i++) {
            tmp[i] = this->array[i];
        }
        delete[] this->array;
        this->array = tmp;
        this->len++;
        this->array[this->tail_pointer++] = val;
    }
    else {
        this->len++;
        this->array[this->tail_pointer++] = val;
    }
}

template <typename T>
int Vector<T>::Size() const {
    return this->len;
}

template <typename T>
bool Vector<T>::IsEmpty() const {
    return this->len == 0;
}

template <typename T>
T& Vector<T>::operator[] (int index) const {
    return this->array[index];
}

#endif // VECTOR_H
